$.config = [];
$.fn.extend({
			datagrid : function(tabObj, paramObj) {
				$(this).html("");
				if (typeof tabObj == "string") {
					tabObj = $.extend($.config[$(this).attr("id")], paramObj);// 表格对象
					console.log(tabObj);
				} else {
					$.config[$(this).attr("id")] = tabObj;// datagrid({});
				}
				var url = tabObj.url;// 请求数据地址
				var lis = tabObj.lis;// 页规格
				var columns = tabObj.columns;// 所有数据列
				var handleColumn = tabObj.handleColumn;
				var dataTrElement = tabObj.dataTrElement;
				var dataTdElement = tabObj.dataTdElement;
				var columnsTrElement = tabObj.columnsTrElement;
				var columnsTdElement = tabObj.columnsTdElement;
				var queryParams = tabObj.queryParams ? tabObj.queryParams : {};// 查询的参数
				var data = tabObj.data;// 本地数据
				var dataHeight = tabObj.dataHeight;// 数据表格高度
				var pagination = tabObj.pagination;// 是否需要分页控件
				var rownumber = tabObj.rownumber;
				var pageBean;
				// !tabObj.pageBean?{eachPageRows:10,currentPageno:1}:tabObj.pageBean;
				var columnMap = [];
				var columnsTr = $(columnsTrElement);
				columnsTr.css("background-color","#A9A9A9");
				if (rownumber) {
					columnsTr.append($(columnsTdElement).css("min-width",
							"58px").text("行号"));
				}
				for ( var i = 0; i < columns.length; i++) {
					var td = $(columnsTdElement);
					td.attr("field", columns[i].field);
					td.text(columns[i].title);
					td.width("200px");
					td.width(columns[i].width);
					console.log(columns[i].width);
					columnsTr.append(td);
					columnMap[columns[i].field] = i;
				}
				if (handleColumn) {
					for ( var i = 0; i < handleColumn.length; i++) {
						var td = $(columnsTdElement);
						td.text(handleColumn[i].title);
						columnsTr.append(td);
					}
				}
				var onDblClickRow = tabObj.onDblClickRow;// 双击行事件
				var handler;
				// 遍历数据
				var t = $(this); // 整个
				// ------------------渲染数据Start--------------------
				var renderDataToHtml = function(ajaxData) {
					var dh = dataHeight == undefined ? 390 + "px"
							: typeof dataHeight == "string" ? dataHeight
									: dataHeight + "px";
					var tableWrap = "<div class='datagrid-warp' style='height:"
							+ dh + "; width:100%;overflow:auto;border:1px solid #f0f0f0;position:relative;'></div>";
					var ttable = t
							.append(tableWrap)
							.find(".datagrid-warp")
							.append(
									"<table class='table table-bordered' ></table>")
							.find("table");
					ttable.append(columnsTr);//表头
//					.append("<thead style='position:absolute;top:0px;'></thead>").find("thead")
//					var thead=ttable.find("thead");
//					var tbody=ttable.append("<tbody style='position:absolute;top:40px;'></tbody>").find("tbody");
//					tbody.css("margin-top","200px");
					for ( var index = 0; index < ajaxData.length; index++) {
						var columnArr = [];
						var obj = ajaxData[index];
						for ( var o in obj) {
							columnArr[columnMap[o]] = obj[o];
						}
						// 一行数据整理完毕
						var tr = $(dataTrElement);
						if (rownumber)
							tr.append($(dataTdElement).append(index + 1));
						for ( var c in columnArr) {
							// formatter
							var td = $(dataTdElement);
							td.dblclick(function(lable) {
								// console.log($(lable.target).parent().index());
								// 双击td 传入当前td的整行数据
								onDblClickRow(ajaxData[$(lable.target).parent()
										.index() - 1]);
							});
							if (!columns[c])
								continue;
							if (columns[c].formatter) {
								// console.log("c："+c);
								// console.log(obj[columns[c].field]);
								// columns[c].formatter()
								tr.append(td.text(columns[c]
										.formatter(obj[columns[c].field])));
							} else {
								tr.append(td.text(columnArr[c]));
							}
							// 设置宽度
							if (columns[c].width)
								td.css("width", columns[c].width);
						}
						if (handleColumn) {
							for ( var i = 0; i < handleColumn.length; i++) {
								var td = $(dataTdElement);
								for ( var j = 0; j < handleColumn[i].contents.length; j++) {
									var a = $("<a style='margin:0 5px;'></a>");
									var handle = handleColumn[i].contents[j].handler;
									// ajaxData[$(lable.target).parent().index()-1]
									a
											.bind(
													"click",
													function() {
														var method = handle;
														return function() {
															$(this)
																	.parent(
																			"td")
																	.trigger(
																			"click");
															method(
																	$(this),
																	ajaxData[$(
																			this)
																			.parent()
																			.parent()
																			.index() - 1],
																	t
																			.attr("id"));
														};
													}());
									td
											.append(a
													.append(handleColumn[i].contents[j].title));
								}
								td.css("min-width", "120px");
								tr.append(td);
							}
						}
						ttable.append(tr);
					}
				};
				// ------------------渲染数据End--------------------

				// ------------------加载数据Start--------------------
				var loadData = function() {
					t.html("");
					if (!url)// url不存在就停止
						return;
					if (!pageBean) {// 第一次进来对查询参数赋默认值，否则根据pageBean走
					// alert("pageBean为空");
						queryParams["eachPageRows"] = lis[0];
						queryParams["currentPageno"] = 1;
					} else {
						queryParams.eachPageRows = pageBean.eachPageRows;
						queryParams.currentPageno = pageBean.currentPageno;
					}
					$.ajax({
						type : "get",
						url : url,
						data : queryParams,
						success : function(result) {
							renderDataToHtml(result.data);
							if (pagination) {
								pageBean = result.pager;
								renderPaginationToHtml();
							}
							t.find("td").click(
									function(label) {
										$(label.target).parents(
												".table-bordered").find("tr")
												.removeClass("table-checked");
										$(label.target).parents("tr").addClass(
												"table-checked");
									});
						}
					});
				};
				// ------------------加载数据End--------------------
				loadData();

				// ------------------渲染分页Start---------------------
				var renderPaginationToHtml = function() {
					// 改变每页条数
					var changePageSize = function(lable) {
						pageBean.eachPageRows = $(lable).data("pagenum");
						var htmlStr = $(lable).data("pagenum") + "条"
								+ "<span class='caret'></span>";
						dropdown.find("button").html(htmlStr);
						loadData();
					};
					// 改变当前页
					var changeCurrPage = function(currPage) {
						pageBean.currentPageno = currPage;
						loadData();
					};
					var paginationWarp = t
							.append(
									"<div class='pagination-warp' style='width:100%;display:flex;margin-top:15px;'></div>")
							.find(".pagination-warp");
					// 分页部分-每页条数
					var dropdown = paginationWarp.append(
							'<div class="dropup" style="flex:2;"></div>').find(
							".dropup");
					// alert(pageBean.eachPageRows);
					var first = '<button class="btn btn-default dropdown-toggle" type="button"\
				id="'
							+ t.attr("id")
							+ '-dropdown" data-toggle="dropdown" aria-haspopup="true"\
					aria-expanded="true">\
					'
							+ pageBean.eachPageRows
							+ '条 <span class="caret"></span>\
				</button>';
					var after = '<ul class="dropdown-menu" aria-labelledby="'
							+ t.attr("id") + '-dropdown"></ul>';
					dropdown.append(first);
					dropdown.append(after);
					for ( var int = 0; int < lis.length; int++) {
						var liA = $("<a href='javascript:void(0);' data-pagenum="
								+ lis[int] + ">" + lis[int] + "条</a>");
						liA.click(function() {
							changePageSize(this);
						});
						var li = $("<li></li>");
						li.append(liA);
						dropdown.find("ul").append(li);
					}
					// 分页部分-页码
					// var pageNum=paginationWarp.append("<div
					// class='pageNum-warp'
					// style='float:left;'></div>");
					var pageNum = paginationWarp
							.append(
									'<div style="flex:5;text-align:center;"><nav>\
			 				<ul class="pagination-ul pagination" style="margin:0;"></ul></nav></div>')
							.find(".pagination-ul");
					var generatePageNum = function(int) {
						var pageNum;
						if (pageBean.currentPageno == int)
							pageNum = '<li class="active"><a href="javascript:void(0);">'
									+ int + '</a></li>';
						else
							pageNum = '<li><a href="javascript:void(0);">'
									+ int + '</a></li>';
						return pageNum;
					};
					/**
					 * 生成上下一页
					 * 
					 * @param currPage
					 * @param pageSize
					 * @returns {___anonymous4666_4715}
					 */
					var generatePrevAndNextPage = function() {
						var currPage = pageBean.currentPageno;
						var pageCount = pageBean.totalPages;
						var firstPage = '<li class="cmp"><a href="javascript:void(0)" class="glyphicon glyphicon-step-backward"></a></li>';
						var prevPage = '<li class="cmp"><a href="javascript:void(0);"  class="glyphicon glyphicon-menu-left"></a></li>';
						var nextPage = '<li class="cmp"><a href="javascript:void(0);"  class="glyphicon glyphicon-menu-right"></a></li>';
						var lastPage = '<li class="cmp"><a href="javascript:void(0);" class="glyphicon glyphicon-step-forward"></a></li>';
						// if(currPage!=1){
						// $(firstPage).on("click","a",function(){
						// alert("第一页");
						// changeCurrPage(1);
						// });
						// $(prevPage).on("click","a",function(){
						// alert("上一页");
						// changeCurrPage(currPage-1);
						// });
						// }
						// if(currPage!=pageCount){
						// $(nextPage).on("click","a",function(){
						// alert("下一页");
						// changeCurrPage(currPage+1);
						// });
						// $(lastPage).on("click","a",function(){
						// alert("最后一页");
						// changeCurrPage(pageCount);
						// });
						// }
						var pageObj = {
							firstPage : firstPage,
							prevPage : prevPage,
							nextPage : nextPage,
							lastPage : lastPage
						};
						return pageObj;
					};
					// --------------------页码部分-------------
					var pageObj = generatePrevAndNextPage();
					pageNum.append(pageObj.firstPage).append(pageObj.prevPage);
					var pageCount = pageBean.totalPages;
					var currPage = pageBean.currentPageno;
					if (pageCount <= 5) {
						for ( var int = 1; int <= pageCount; int++) {
							var pn = generatePageNum(int);
							pageNum.append(pn);
						}
					} else {
						var int;// 页码值(固定5页中最小的值)
						var evalStr;// 动态执行的code(固定5页中最大的值)
						if (currPage > pageCount - 2) {
							int = pageCount - 4;
							evalStr = "int<=pageCount";
						} else if (currPage > 2 && currPage <= pageCount - 2) {
							int = currPage - 2;
							evalStr = "int<=currPage+2";
						} else {
							int = 1;
							evalStr = "int<=5";
						}
						for (int; eval(evalStr); int++) {
							var pn = generatePageNum(int);
							pageNum.append(pn);
						}
					}
					pageNum.append(pageObj.nextPage).append(pageObj.lastPage);
					// --------------------页码部分-------------
					// --------------------上下页事件-------------
					if (currPage != 1) {
						t.find(".pagination-ul").find(".cmp:eq(0)").find("a")
								.bind("click", function() {
									changeCurrPage(1);
								});
						t.find(".pagination-ul").find(".cmp:eq(1)").find("a")
								.bind("click", function() {
									changeCurrPage(currPage - 1);
								});
					}
					if (currPage != pageCount) {
						t.find(".pagination-ul").find(".cmp:eq(2)").find("a")
								.bind("click", function() {
									changeCurrPage(currPage + 1);
								});
						t.find(".pagination-ul").find(".cmp:eq(3)").find("a")
								.bind("click", function() {
									changeCurrPage(pageCount);
								});
					}
					t.find(".pagination-ul>li:not(.cmp,.active)").find("a")
							.bind("click", function() {
								changeCurrPage($(this).text());
							});
					// --------------------上下页事件-------------
					// pageNum.append(generatePrevAndNextPage().nextPage);
					// 判断上下一页是否可用
					if (currPage == 1) {
						pageNum.find("li:eq(0)").addClass("disabled");
						pageNum.find("li:eq(1)").addClass("disabled");
					} else {
						pageNum.find("li:eq(0)").removeClass("disabled");
						pageNum.find("li:eq(1)").removeClass("disabled");
					}
					if (currPage == pageCount) {
						pageNum.find("li").last().addClass("disabled");
						pageNum.find("li").last().prev().addClass("disabled");
					} else {
						pageNum.find("li").last().removeClass("disabled");
						pageNum.find("li").last().prev()
								.removeClass("disabled");
					}

					/**
					 * 跳转指定页部分
					 */
					var toPage = paginationWarp
							.append(
									'<div class="input-group"style="flex:2;"><input type="text" style="width:100px;" class="form-control" placeholder="共'
											+ pageBean.totalPages
											+ '页跳转到..."> \
					<span class="input-group-btn"> \
							<button class="btn btn-default goPage" type="button">Go!</button> \
						</span></div>')
							.find(".input-group");
					toPage.find("button").bind(
							"click",
							function() {
								// alert($(this).parent().prev(".form-control").val());
								var toNum = $(this).parent().prev(
										".form-control").val();
								if (toNum > pageBean.totalPages || toNum < 1) {
									return;
								}
								changeCurrPage($(this).parent().prev(
										".form-control").val());
							});
				};
				// ------------------渲染分页End---------------------

			},
			myform : function(tabObj) {
				var $this=$(this);//整个Jquery对象
				var url=tabObj.url;//暂定 
				var actionUrl=tabObj.actionUrl;
				var lable="<lable></lable>";
				var input="<input type='text' class='form-control' />";
				var fields=tabObj.fields;
				var data=tabObj.data;//传入的数据
				var queryParams=tabObj.queryParams;
				/**
				 * 渲染样式
				 * @returns
				 */
				var renderToHtml=function(){
					$this.html("");
					if(!fields)
						return;
					var form=$this.append("<form role='form' action="+actionUrl+" method='post'><form>").find("form");
					for ( var i = 0; i < fields.length; i++) {
						var group=form.append("<div class='form-group' style='width:220px;'></div>").find(".form-group").eq(i);
						var lable1=$(lable);
						var input1=$(input);
						lable1.attr("for",fields[i].field).text(fields[i].lable+"：");
						input1.attr("id",fields[i].field);
						input1.attr("name",fields[i].field);
						input1.val(fields[i].value);
						if(fields[i].readonly)
							input1.attr("readonly","readonly");
						if(fields[i].disabled)
							input1.attr("disabled","disabled");
						if(fields[i].isHide==1)
							group.hide();
						group.append(lable1).append(input1);
					}
				};
				
				/**
				 * 加载数据到form表单中
				 * @returns
				 */
				var loadData=function(){
					if(url){
						$.ajax({
							type:"get",
							url:url,
							data:queryParams,
							success:function(result){
								fields=result;
								renderToHtml();
							}
						});
					}else{
						renderToHtml();
					}
				};
				loadData();
			}
		});

/*
 *<div>
 *	<form>
 *	<div>
 *	<lable>编号：</lable>
 *<input value="1"/>
 *	<lable>表注释：</lable>
 *<input value="什么什么表"/>
 *	<lable>名字：</lable>
 *<input value="什么什么名字"/>
 *</div>
 *</form>
 *</div> 
 *
 *$this=<div></div>
 */
